package com.hdoit.kylin.web.group.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.group.model.Task;
import com.hdoit.kylin.logical.group.service.ITaskService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.hdoit.kylin.logical.viewmodel.TaskSearch;
import com.hdoit.kylin.web.common.CommonWebUtils;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;


/**
 * @author      liyaochong
 * @email       
 * @createtime  2017年08月01日 15:26:04
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/calendar/task/")
public class TaskController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/calendar/task/";
    }

    private ITaskService taskService;
    public ITaskService getTaskService() {
        return taskService;
    }
    @Resource
    public void setTaskService(ITaskService taskService) {
        this.taskService = taskService;
    }
    
    private IStudentService studentService;
	public IStudentService getStudentService(){
		return studentService;
	}
	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService=studentService;
	}
	
	private ITeacherService teacherService;
	public ITeacherService getTeacherService(){
		return teacherService;
	}
	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService=teacherService;
	}

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="taskDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,TaskSearch search,boolean ajaxPost){
    	if(CommonWebUtils.isTeacher(getLoginModel()))
	    	search.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
		else if(CommonWebUtils.isStudent(getLoginModel()))
    		search.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
        pagerModel.setTotalAmount(taskService.queryCountByGroupId(search));
        List<Task> tasks= taskService.queryListByGroupId(search,pagerModel);
        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("tasks", tasks);
        model.addAttribute("taskSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String taskDetailed(Model model,String key){
        Task task=new Task();
        if(!StringUtils.isEmpty(key)){
            task=taskService.queryModel(key);
        }
        model.addAttribute("task", task);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Task task){
    	if(StringUtils.isEmpty(task.getTaskId())){
			task.setTaskId(CommonUtils.getKeyId());
			if(CommonWebUtils.isTeacher(getLoginModel()))
				task.setGroupId(teacherService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			else if(CommonWebUtils.isStudent(getLoginModel()))
				task.setGroupId(studentService.queryGroupIdByLoginId(getLoginModel().getLoginId()));
			task.setModelStatus(ModelStatus.add);
		}else{
			task.setModelStatus(ModelStatus.update);
		}
        ActionResult actionResult;
        if(taskService.applyChange(task)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("task", task);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(taskService.deleteModel(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}